//////////////////////////////////////////////////////////////////////////////////
//
// ActionScript 3.0 Common Library (MIT License)
// Copyright (c) 2009 AS3CommonCode | Tony Birleffi | http://www.mosaiceye.com
// 
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////////

package org.as3commoncode.command
{
	import org.as3commoncode.command.AbstractCommand;
	
	/**
	 * Use this class to delegate commands.
	 * 
	 * @see	org.as3commoncode.command.AbstractCommand;
	 * @author Tony Birleffi
	 */
	public class DelegateCommand extends AbstractCommand
	{
		private var scope:Object;
		private var funcToRun:Function;
		private var argArray:Array;
		private var funcNameToRun:String;
		
		/**
		 * Constructor.
		 * 
		 * @param scope1	Object.
		 * @param func1		Function.
		 * @param args1		Array.
		 */
		public function DelegateCommand(scope1:Object, func1:Function, args1:Array)
		{
			argArray = new Array();
			setScope(scope1);
			setFunction(func1);
			if(args1 != null) setArgs(args1);
		}
		
		/**
		 * Create a delegate.
		 * 
		 * @param scope1	Object.
		 * @param func1		Function.
		 * @param args1		Array.
		 */
		public static function create(scope1:Object, func1:Function, args1:Array):DelegateCommand
		{
			return new DelegateCommand(scope1, func1, args1);
		}
		
		/**
		 * Execute.
		 */
		override public function execute():void
		{
			// HELP - any idea how one might elegantly tell when this function is actually done?
			funcToRun.apply(scope, argArray);
			this.success();
		}
		
		/**
		 * Set the command scope.
		 * 
		 * @param obj	Object.
		 */
		public function setScope(obj:Object):void
		{
			scope = obj;
		}
		
		/**
		 * Set the function.
		 * 
		 * @param func	Function.
		 */
		public function setFunction(func:Function):void
		{
			this.funcToRun = func;
		}
		
		/**
		 * Set the function name.
		 * 
		 * @param func	String.
		 */
		public function setFunctionName(func:String):void
		{
			this.funcNameToRun = func;
		}
		
		/**
		 * Set the arguments array.
		 * 
		 * @param args	Array.
		 */
		public function setArgs(args:Array):void
		{
			this.argArray = args;
		}
	}
}